package com.wei.frame.utils.rx;

import com.wei.frame.mvp.IView;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.ObservableTransformer;
import io.reactivex.rxjava3.schedulers.Schedulers;


/**
 * 作者:赵若位
 * 时间:2020/8/20 11:24
 * 功能:RxJava工具类
 */
public class RxUtils {
    private RxUtils() {
        throw new IllegalStateException("You can't instantiate me~");
    }


    /*RxJava展示进度条*/
    public static <T> ObservableTransformer<T, T> applyLoading(IView view) {
        return observable -> observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> {
                    if (null != view) {
                        view.showLoading();
                    }
                }).observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    if (null != view) {
                        view.hideLoading();
                    }
                }).unsubscribeOn(Schedulers.io());
    }

    /*RxJava线程切换*/
    public static <T> ObservableTransformer<T, T> getNormalScheduler() {
        return upstream -> upstream.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io());
    }

}
